<script lang="ts" setup>
import type {FormInstance, FormRules} from 'element-plus';
import * as projectTaskApi from "@/api/business/project/projectTask";
import {DICT_TYPE, getDictOptions} from "@/utils/dict";
import {useOptionStore} from "@/store/modules/selectOption";
import {storeToRefs} from "pinia";

defineOptions({name: "ProjectTaskForm"})
const {t} = useI18n() // 国际化
const message = useMessage()
const emit = defineEmits(['success'])
const optionStore = useOptionStore()
const {userOption} = storeToRefs(optionStore)


const disabled = computed(() => {
  return type.value === 'detail'
})
const type = ref('detail')
const title = ref('详情')
const dialogVisible = ref(false)
const formLoading = ref(false)


const formRef = useTemplateRef<FormInstance>('formRef')
const form = ref({
  taskName: void 0,//项目名称
  director: void 0,
  score: 0,
  state: false,
  content: void 0,
  remake: void 0,
})
const rules = ref<FormRules>({
  taskName: [
    {required: true, message: '请输入任务名称', trigger: 'blur'},
  ],
  director: [
    {required: true, message: '请输选择负责人', trigger: 'change'},
  ],
  score: [
    {required: true, message: '请输入分值', trigger: 'blur'},
  ],
  state: [
    {required: true, message: '请选择状态', trigger: 'change'},
  ],
  content: [
    {required: true, message: '请输入任务内容', trigger: 'blur'},
  ],
  remark: [
    {required: true, message: '请输入备注', trigger: 'blur'},
  ],
})

function handleClose() {
  dialogVisible.value = false
  formRef.value?.resetFields()
  form.value = {
    taskName: void 0,//项目名称
    director: void 0,
    score: 0,
    state: false,
    content: void 0,
    remake: void 0,
  }
}

async function open(state: 'add' | 'edit' | 'detail' = "detail", id?: string) {
  type.value = state
  title.value = t('action.' + state)
  dialogVisible.value = true
  if (id) {
    formLoading.value = true
    try {
      form.value = await projectTaskApi.getProjectTask(id)
    } finally {
      formLoading.value = false
    }
  }
}

async function handleSubmit() {
  try {
    await formRef.value?.validate()
    if (type.value === 'add') {
      await projectTaskApi.createProjectTask(form.value)
      message.success(t('common.createSuccess'))
    } else if (type.value === 'edit') {
      await projectTaskApi.updateProjectTask(form.value)
      message.success(t('common.updateSuccess'))
    }
    emit('success')
    handleClose()
  } catch (e) {
    console.log(e)
  }
}

defineExpose({
  open
})
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    :title
    top="5vh"
    width="50%"
    @close="handleClose"
  >
    <el-form ref="formRef" v-loading="formLoading" :inline="true" :model="form"
             :rules class="-mb-15px" label-width="auto">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item class="w-100%" label="任务名称" prop="taskName">
            <el-input v-model="form.taskName" :disabled clearable placeholder="请输入项目名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="w-100%" label="负责人" prop="director">
            <el-select v-model="form.director" :disabled clearable
                       placeholder="请选择负责人">
              <el-option
                v-for="item in userOption"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="w-100%" label="状态" prop="state">
            <el-switch v-model="form.state" :disabled></el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="w-100%" label="分值" prop="score">
            <el-input-number v-model="form.score" :disabled :max="1" :min="0"
                             :step="0.1"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="w-100%" label="任务内容" prop="content">
            <el-input v-model="form.content" :disabled clearable placeholder="请输入备注"
                      type="textarea"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="w-100%" label="备注" prop="remark">
            <el-input v-model="form.remark" :disabled clearable placeholder="请输入备注"
                      type="textarea"/>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button v-show="type!=='detail'" type="primary" @click="handleSubmit">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>

</style>
